import Vue from "vue";
import Vuex from "vuex";
import { setGlobalData } from "@/initMicroApps";
import { login, getResource } from "@/api";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    appConfig: null,
    userInfo: {},
    resource: [],
    curConfig: {}
  },
  mutations: {
    ['SET_CURCONFIG'](state, data) {
      state.curConfig = data;
    },
    appConfig(state, data) {
      state.appConfig = data;
    },
    userInfo(state, data) {
      state.userInfo = data;
    },
    resource(state, data) {
      state.resource = data;
    }
  },
  actions: {
    async userLogin({ commit }, param) {
      let res = await login(param)
      localStorage.setItem("token", res.token);
      localStorage.setItem("userInfo", JSON.stringify(res));

      commit("userInfo", res);

      setGlobalData(res, null, null);

    },
    logOut({ commit }) {
      localStorage.setItem("token", "");
      localStorage.setItem("userInfo", "");
      commit("userInfo", {});
      commit("resource", []);
    },
    async getResource({ commit }) {
      let resource = await getResource();
      commit("resource", resource);
      setGlobalData(null, null, resource);
    }
  },
  modules: {},
  getters: {
    appConfig: state => state.appConfig,
    userInfo: state => state.userInfo,
    resource: state => state.resource,
    curConfig: state => state.curConfig
  }
});
